package com.rumor.background.Controller;

import com.rumor.background.Mapper.RumorContributeMapper;
import com.rumor.background.Utils.Contribute.ContributeUtils;
import com.rumor.background.Utils.Utils;
import com.rumor.background.pojo.RumorContribute;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@RestController
public class RumorContributeController {

    @Autowired
    RumorContributeMapper mapper;


    @GetMapping("/getRumorCode")
    public String getRumorCode(){
        String rumor_code = Utils.getRumorCode();
        RumorContribute rc = mapper.queryContributeByRumorCode(rumor_code);
        while(rc != null){
            rumor_code = Utils.getRumorCode();
            rc = mapper.queryContributeByRumorCode(rumor_code);
        }
        return rumor_code;
    }


    @PostMapping("/rumorImgUpload")
    public int rumorImgUpload(HttpServletRequest request, HttpServletResponse response) throws IOException, InterruptedException {
        File directory = new File("RUMOR");
        String username = request.getParameter("username");
        String rumor_code = request.getParameter("rumor_code");
        String filename = request.getParameter("filename");
        RumorContribute RC = mapper.queryContributeByRumorCode(rumor_code);

        // 如果数据库中存在该随机编码，直接将图片保存到对应位置
        if(RC != null){
            // 如果open_id不一样，可能出了问题，投稿失败
            if(!username.equals(RC.getUsername())) return -1;
            // 保存图片
            String path = directory.getCanonicalPath() + "/" +  RC.getImg_location();
            Utils.saveImgFile(request, path, filename);
            return 1;
        }

        // 如果不存在该随机编码对应的投稿，创建图片保存路径并保存图片到本地，数据库中记录投稿信息
        else{
            String date = ContributeUtils.getDate();
            String img_location = date + "/" + rumor_code + "/";
            RC = new RumorContribute(username, rumor_code, null, img_location, date, "-1");
            String path = directory.getCanonicalPath() + "/" +  img_location;
            Utils.saveImgFile(request, path, filename);
            return mapper.addRumorContribute(RC);
        }
    }

    @PostMapping("/deleteRumorImg")
    public int deleteRumorImg(@RequestParam("username") String username, @RequestParam("rumor_code") String rumor_code,
                              @RequestParam("filename") String filename) throws IOException {
        RumorContribute rc = mapper.queryContributeByRumorCode(rumor_code);
        if(!rc.getUsername().equals(username)) return -1;
        File directory = new File("RUMOR");
        String path = directory.getCanonicalPath() + "/" + rc.getImg_location();
        return Utils.deleteImgFile(path, filename + ".jpg");
    }

    @PostMapping("/rumorSubmit")
    public int rumorSubmit(@RequestParam("username") String username, @RequestParam("rumor_code") String rumor_code,
                           @RequestParam(value = "remark", required = false) String remark){
        RumorContribute rc = mapper.queryContributeByRumorCode(rumor_code);
        // 如果投稿不存在或open_id不一样，可能出了问题，投稿失败
        if(rc == null || !username.equals(rc.getUsername())) return -1;
        remark = (remark == null || remark.length() == 0)?"此投稿无备注":remark;
        rc.setRemark(remark);
        rc.setMark("0");
        return mapper.updateRumorContribute(rc);
    }

}
